#include <stdio.h>
#include <math.h>
#include <stdlib.h>

//opendir fdopendir
#include <dirent.h>

//getuid getegid
#include <unistd.h>
#include <sys/types.h>

//signal
#include <signal.h>

void UserIDAndGroupID()
{
  printf("***** getuid/geteuid and getgid/getegid test\n");
  printf("uid =  %d,gid =  %d\n",getuid(),getgid());
  printf("euid = %d,egid = %d\n",geteuid(),getegid());
  printf("***** end\n");
}
void ListAllTheFiles();

void SigMain();
void sig_usr(int signo);



int main(int argc,char **argv)
{
  UserIDAndGroupID();
  ListAllTheFiles();
  SigMain();
  return 0;
}

void ListAllTheFiles()
{
  printf("***** ListAllTheFiles\n");
  DIR *dp;
  struct dirent *dirp;
  char *path = ".";

  if((dp = opendir(path)) == NULL) 
    {
      printf("can't open %s",path);
      exit(1);
    }

  while((dirp = readdir(dp)) != NULL)
    {
      printf("%s\n",dirp -> d_name); 
    }

  closedir(dp);
  printf("***** end\n");
}

void SigMain()
{
  if(signal(SIGUSR1,sig_usr) == SIG_ERR) 
    {
      printf("Can't catch SIGUSR1");
      exit(1);
    }
  if(signal(SIGUSR2,sig_usr) == SIG_ERR) 
    {
      printf("can't catch SIGUSR2");
      exit(1);
    }

  for(;;)
    pause();
}

void sig_usr(int signo)
{
  if(signo == SIGUSR1) 
    {
      printf("received SIGUSR1\n");
    }
  else if(signo == SIGUSR2)  
    {
      printf("received SIGUSR2\n");
    }
  else 
    {
      printf("received signal %d\n",signo);
      exit(1);
    }
}
